package com.dzh.healthflutter.flutter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.File;
import java.lang.reflect.Field;

import io.flutter.embedding.engine.loader.FlutterLoader;

/**
 * @Description: 自定义flutter loader
 * @Author: duzhenhua3
 * @CreateDate: 7/30/21 7:35 PM
 */
class DzhFlutterLoader extends FlutterLoader {

    public static String FIX_NAME = "libappfix.so";
    public static DzhFlutterLoader instance = new DzhFlutterLoader();

    private DzhFlutterLoader() {

    }

    public static DzhFlutterLoader getInstance() {
        return instance;
    }

    @Override
    public void ensureInitializationComplete(@NonNull Context applicationContext, @Nullable String[] args) {

        try {
            File filesDir = applicationContext.getFilesDir();
            File file = new File(filesDir, FIX_NAME);
            if (file.exists()) {
                Class<FlutterLoader> aClass = FlutterLoader.class;
                Field field = aClass.getDeclaredField("aot-shared-library-name");
                field.setAccessible(true);
                field.set(aClass, FIX_NAME);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.ensureInitializationComplete(applicationContext, args);
    }
}
